package com.gome.axwl.aoc.web.controller.quartzJob;

import com.gome.axwl.aoc.common.util.DateUtil;
import com.gome.axwl.aoc.service.GradeRuleService;
import com.gome.axwl.aoc.service.IntegrationRuleService;
import com.gome.commons.core.ErrorCode;
import com.gome.commons.core.ResultData;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * @Description: 积分等级规则定时任务
 * @Auther: yinyuxin@gome.com.cn
 * @Date: 2018/5/2 16:02
 */
@Component
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class RuleInitJob implements Job {

    private static final Logger LOGGER=LoggerFactory.getLogger(RuleInitJob.class);

    @Autowired
    private IntegrationRuleService integrationRuleService;
    @Autowired
    private GradeRuleService gradeRuleService;

    /**
     * 初始化当月的规则设置
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        LOGGER.info("quartz定时初始化积分规则和等级规则信息任务开始执行,时间{}",DateUtil.date2String(new Date(),DateUtil.FORMAT_DEFAULT));
        ResultData ruleResultData = integrationRuleService.initIntegrationRules();
        if (!Objects.equals(ErrorCode.SUCCESS,ruleResultData.getCode())){
            throw new RuntimeException("初始化积分规则设置失败，详情:"+ruleResultData.getMessage());
        }
        ResultData gradeResultDate = gradeRuleService.initGradeRules();
        if (!Objects.equals(ErrorCode.SUCCESS,gradeResultDate.getCode())){
            throw new RuntimeException("初始化积分规则设置失败，详情:"+gradeResultDate.getMessage());
        }
    }
}
